﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BallWar
{
    public partial class Form1 : Form
    {
        Graphics g;
        Sence sence = new Sence();
        Game game1;
        Game game2;
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            g = this.CreateGraphics();
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;  //图片柔顺模式选择
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;//高质量
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;//再加一点
            g.Clear(BackColor);
            game1 = new Game(g, sence);
            game2 = new Game(g, sence);
            timer1.Enabled = true;
            timer2.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            game1.ball.CheckCollision(Size);
            if (game1.ball.Moving) game1.Move();
            string a = string.Format("玩家一分数：{0} 玩家二分数：{1}", game1.Mark.ToString(), game2.Mark.ToString());
            Text = "球球小作战" + a;
            timer1.Interval = game1.Speed;
            Random r = new Random();
            if (sence.Beans.Count < 50)
            {
                Bean bean = new Bean(Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)), new Point(r.Next(5, Width), r.Next(5, Height)), g);
                sence.Beans.Add(bean);
                bean.Display();
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            
            if (game2.ball.Moving) game2.Move();
            if (!(game2.ball._station.X + game2.ball._size.Width / 2 >= game2.ball._size.Width / 2 && game2.ball._station.X + game2.ball._size.Width / 2 <= Width - game2.ball._size.Width/2  && game2.ball._station.Y + game2.ball._size.Width / 2 >= game2.ball._size.Width / 2 && game2.ball._station.Y + game2.ball._size.Width / 2 <= Height - game2.ball._size.Width ))
                game2.ball.Moving = false;
            timer2.Interval = game2.Speed;
            Random r = new Random();
            if (game1.beans.Count < 50)
            {
                Bean bean = new Bean(Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)), new Point(r.Next(5, Width), r.Next(5, Height)), g);
                sence.Beans.Add(bean);
                bean.Display();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.W)
            {
                if (!game1.ball.Moving)
                {
                    if (game1.ball._direction == Direction.East)
                    {
                        game1.ball._direction = Direction.West;
                        game1.Move();
                    }
                    else if (game1.ball._direction == Direction.West)
                    {
                        game1.ball._direction = Direction.East;
                        game1.Move();
                    }
                }
                if (game1.ball._direction != Direction.North)
                    game1.ball.Moving = true;
                game1.Turn(Direction.North);
            }
            if (keyData == Keys.S)
            {
                if (!game1.ball.Moving)
                {
                    if (game1.ball._direction == Direction.East)
                    {
                        game1.ball._direction = Direction.West;
                        game1.Move();
                    }
                    else if (game1.ball._direction == Direction.West)
                    {
                        game1.ball._direction = Direction.East;
                        game1.Move();
                    }
                }
                if (game1.ball._direction != Direction.South)
                    game1.ball.Moving = true;
                game1.Turn(Direction.South);
            }
            if (keyData == Keys.A)
            {
                if (!game1.ball.Moving)
                {
                    if (game1.ball._direction == Direction.North)
                    {
                        game1.ball._direction = Direction.South;
                        game1.Move();
                    }
                    else if (game1.ball._direction == Direction.South)
                    {
                        game1.ball._direction = Direction.North;
                        game1.Move();
                    }
                }
                if (game1.ball._direction != Direction.West)
                    game1.ball.Moving = true;
                game1.Turn(Direction.West);
            }
            if (keyData == Keys.D)
            {
                if (!game1.ball.Moving)
                {
                    if (game1.ball._direction == Direction.North)
                    {
                        game1.ball._direction = Direction.South;
                        game1.Move();
                    }
                    else if (game1.ball._direction == Direction.South)
                    {
                        game1.ball._direction = Direction.North;
                        game1.Move();
                    }
                }
                if (game1.ball._direction != Direction.East)
                    game1.ball.Moving = true;
                game1.Turn(Direction.East);
            }

            if (keyData == Keys.Up)
            {
               
                game2.Turn(Direction.North);
                if (game2.ball.Down)
                {
                    game2.ball.Move();
                }
            }
            if (keyData == Keys.Down)
            {
                if (!game2.ball.Moving)
                {
                    if (game2.ball._direction == Direction.East)
                    {
                        game2.ball._direction = Direction.West;
                        game2.Move();
                    }
                    else if (game2.ball._direction == Direction.West)
                    {
                        game2.ball._direction = Direction.East;
                        game2.Move();
                    }
                }
                if (game2.ball._direction != Direction.South)
                    game2.ball.Moving = true;
                game2.Turn(Direction.South);
               
            }
            if (keyData == Keys.Left)
            {
                if (!game2.ball.Moving)
                {
                    if (game2.ball._direction == Direction.North)
                    {
                        game2.ball._direction = Direction.South;
                        game2.Move();
                    }
                    else if (game2.ball._direction == Direction.South)
                    {
                        game2.ball._direction = Direction.North;
                        game2.Move();
                    }
                }
                if (game2.ball._direction != Direction.West)
                    game2.ball.Moving = true;
                game2.Turn(Direction.West);
                
            }
            if (keyData == Keys.Right)
            {
                if (!game2.ball.Moving)
                {
                    if (game2.ball._direction == Direction.North)
                    {
                        game2.ball._direction = Direction.South;
                        game2.Move();
                    }
                    else if (game2.ball._direction == Direction.South)
                    {
                        game2.ball._direction = Direction.North;
                        game2.Move();
                    }
                }
                if (game2.ball._direction != Direction.East)
                    game2.ball.Moving = true;
                game2.Turn(Direction.East);
                
            }
            return true;
        }

       
    }
}
